#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
int a[N], b[N], f[N][N];
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) cin >> b[i];
  for (int i = 1; i <= n; ++i) {
    int maxv = 0;
    for (int j = 1; j <= n; ++j) {
      f[i][j] = f[i - 1][j];
      if (a[i] == b[j]) f[i][j] = max(maxv + 1, f[i][j]);
      if (a[i] > b[j]) maxv = max(maxv, f[i - 1][j]);
    }
  }
  int res = 0;
  for (int i = 1; i <= n; ++i) res = max(res, f[n][i]);
  cout << res << endl;
}
